package com.xjy.system.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;

/**
 * @author hlj
 * @version 1.0
 * @date 2021/4/1 00:19
 */
@Data
@ApiModel(value = "分页返回对象")
public class CommonPage<T> {

    @ApiModelProperty("页码")
    private Long pageNum;

    @ApiModelProperty("每页条数")
    private Long pageSize;

    @ApiModelProperty("数据列表")
    private List<T> list;

    @ApiModelProperty("总条数")
    private Long totalSize;

    @ApiModelProperty("总页数")
    private Long pages;

    public static <T> CommonPage<T> page(IPage<T> page) {
        CommonPage<T> commonPage = new CommonPage();
        commonPage.setPageNum(page.getCurrent());
        commonPage.setPageSize(page.getSize());
        commonPage.setList(page.getRecords());
        commonPage.setTotalSize(page.getTotal());
        commonPage.setPages(page.getPages());
        return commonPage;
    }

    public static <T> CommonPage<T> pageChangePojo(IPage page, Class<T> destinationClass){
        CommonPage<T> commonPage = new CommonPage();
        commonPage.setPageNum(page.getCurrent());
        commonPage.setPageSize(page.getSize());
        commonPage.setTotalSize(page.getTotal());
        commonPage.setPages(page.getPages());
        List<T> list = BeanMapper.mapList(page.getRecords(), destinationClass);
        commonPage.setList(list);
        return commonPage;
    }

}
